import path from "path";
import fse from "fs-extra";


console.log("start running");

const __dirname = path.resolve(path.dirname(''));

type TypeData = {
	"0": string;
	动词?: string[];
	指示词?: string[];
	形容词?: string[];
	副词?: string[];
	名词?: string[];
	虚词?: string[];
	代词?: string[];
	关联?: string[];
};

export const keyData: Array<keyof Omit<TypeData, "0">> = [
	"动词",
	"名词",
	"指示词",
	"形容词",
	"副词",
	"虚词",
	"代词",
	"关联",
];

let repeatedWords = 0;
const file = path.resolve(__dirname, "./public/dictionary.json");
const data: TypeData[] = fse.readJSONSync(file);

const textList: string[] = [];
const updateData: TypeData[] = [];

data.forEach((item) => {
	let word = item["0"].toLowerCase();

	if (textList.includes(word)) {
		word = "$" + word;
		repeatedWords++;
	}

	keyData.forEach((name) => {
		if (item[name] !== undefined) {
			item[name] = item[name]!.sort();
		}
	});

	updateData.push({
		...item,
		0: word,
	});

	textList.push(word);
});

console.log("The count of this dictionary is " + textList.length);

console.log("The count of repeated word is " + repeatedWords);

fse.writeJSONSync(
	file,
	updateData.sort((a, b) => a["0"].localeCompare(b["0"]))
);
